import { CoolController, BaseController } from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { ProviderDepositEntity } from '../../entity/deposit';
import { ProviderDepositService } from '../../service/deposit';
import {
  PayDepositDTO,
  RefundDepositDTO,
  DepositRecordPageDTO,
} from '../../dto/deposit';

/**
 * 服务人员质保金管理
 */
@CoolController({
  api: [],
  entity: ProviderDepositEntity,
  service: ProviderDepositService,
})
export class ProviderDepositController extends BaseController {
  @Inject()
  ctx: Context;

  @Inject()
  depositService: ProviderDepositService;

  /**
   * 获取当前质保金余额
   */
  @Post('/balance', { summary: '获取质保金余额' })
  async balance() {
    const balance = await this.depositService.getBalance(this.ctx.provider.id);
    return this.ok({ balance });
  }

  /**
   * 获取质保金统计信息
   */
  @Post('/statistics', { summary: '获取质保金统计' })
  async statistics() {
    const data = await this.depositService.getStatistics(this.ctx.provider.id);
    return this.ok(data);
  }

  /**
   * 创建缴纳质保金订单
   */
  @Post('/pay', { summary: '缴纳质保金' })
  async pay(@Body() dto: PayDepositDTO) {
    const result = await this.depositService.createPayOrder(
      this.ctx.provider.id,
      dto
    );
    return this.ok(result);
  }

  /**
   * 模拟支付成功（测试用）
   */
  @Post('/mockPaySuccess', { summary: '模拟支付成功' })
  async mockPaySuccess(@Body('orderNo') orderNo: string) {
    const result = await this.depositService.mockPaySuccess(
      this.ctx.provider.id,
      orderNo
    );
    return this.ok(result);
  }

  /**
   * 申请退还质保金
   */
  @Post('/refund', { summary: '申请退还质保金' })
  async refund(@Body() dto: RefundDepositDTO) {
    const result = await this.depositService.refund(this.ctx.provider.id, dto);
    return this.ok(result);
  }

  /**
   * 质保金记录列表
   */
  @Post('/recordPage', { summary: '质保金记录列表' })
  async recordPage(@Body() query: DepositRecordPageDTO) {
    const result = await this.depositService.recordPage(
      this.ctx.provider.id,
      query
    );
    return this.ok(result);
  }
}

